package com.flea.hclv.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.flea.hclv.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.flea.hclv.model.entity.HDriver;
import com.flea.hclv.model.entity.HDriverLicense;
import com.flea.hclv.model.entity.HResourceRelation;
import com.flea.hclv.service.HDriverService;
import com.flea.system.bean.FleaPage;
import com.flea.system.entity.OrgDept;
import com.flea.system.platform.session.FleaOrgUtils;

/**
 * @program: project
 * @description: 司机
 * @author: shiliqiang
 * @create: 2018-09-26 20:42
 * @version: 1.0
 */
@Controller
@RequestMapping("/flea/hclv/driver/")
public class HDriverController {

    @Autowired
    private HDriverService hDriverService;

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public ServerResponse<String> saveOrUpdate(HttpServletRequest request){
        String userId = FleaOrgUtils.getCurUserUuid(request);
        OrgDept currentUserDept = FleaOrgUtils.getCurrentUserDept(request);
        String model = request.getParameter("model");
        String images = String.valueOf(JSON.parseObject(model).getJSONArray("images"));
        String license = String.valueOf(JSON.parseObject(model).get("license"));
        List<HResourceRelation> hResourceRelations = JSON.parseArray(images,HResourceRelation.class);
        HDriverLicense hDriverLicense = JSON.parseObject(license,HDriverLicense.class);
        HDriver driver = JSON.parseObject(model,HDriver.class);
        return hDriverService.saveOrUpdate(userId,currentUserDept,driver,hResourceRelations,hDriverLicense);
    }
    /**
     * 查找平台所有司机  (供平台管理员使用)
     * @param request request
     * @param paging 分页对象
     * @param name 司机姓名
     * @param type 证件号
     * @return list
     */
    @ResponseBody
    @RequestMapping("selectAll")
    public ServerResponse<FleaPage<HDriver>> selectAll(HttpServletRequest request,FleaPage<HDriver> paging,
                                            @RequestParam(name = "name",defaultValue = "")String name,
                                            @RequestParam(name = "type",defaultValue = "")String type){
        return hDriverService.selectAll(paging.getCurPage(),paging.getPageSize(),name,type);
    }

    /**
     * 通过部门查找司机
     * @param request request
     * @param paging 分页对象
     * @param name 司机姓名
     * @param type 证件号
     * @return list
     */
    @ResponseBody
    @RequestMapping("selectByDeptId")
    public ServerResponse<FleaPage<HDriver>> selectByDeptId(HttpServletRequest request,FleaPage<HDriver> paging,
                                                       @RequestParam(name = "name",defaultValue = "")String name,
                                                       @RequestParam(name = "type",defaultValue = "")String type){
        OrgDept currentUserDept = FleaOrgUtils.getCurrentUserDept(request);
        //type  这里是证件号
        return hDriverService.selectByDeptId(currentUserDept,paging.getCurPage(),paging.getPageSize(),name,type);
    }
    @ResponseBody
    @RequestMapping("deleteByIds")
    public ServerResponse<String> deleteByIds(HttpServletRequest request, @RequestParam("ids")String ids){
        String userId = FleaOrgUtils.getCurUserUuid(request);
        List<String> idList = JSON.parseArray(ids,String.class);
        return hDriverService.deleteByIds(userId,idList);
    }

    @ResponseBody
    @RequestMapping("selectLicenseAndImage")
    public ServerResponse<JSONObject> selectLicenseAndImage(String driverId){
        return  hDriverService.selectLicenseAndImage(driverId);
    }
    
}
